package com.mindainfo.groundwave.server.equipment.service;

import com.mindainfo.groundwave.common.api.equipment.param.RelayParam;
import com.mindainfo.groundwave.common.api.equipment.param.RelayQueryListParam;
import com.mindainfo.groundwave.common.api.equipment.service.IRelayService;
import com.mindainfo.groundwave.common.api.equipment.vo.RelayQueryListVo;
import com.mindainfo.groundwave.common.api.equipment.vo.RelayVo;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.RedisConstant;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.common.bean.TableData;
import com.mindainfo.groundwave.common.utils.ValidateUtil;
import com.mindainfo.groundwave.server.base.annotation.NeedValidate;
import com.mindainfo.groundwave.server.base.utils.SysUtil;
import com.mindainfo.groundwave.server.equipment.bean.RelayInfo;
import com.mindainfo.groundwave.server.equipment.bean.RelayInstall;
import com.mindainfo.groundwave.server.equipment.mapper.RelayInfoMapper;
import com.mindainfo.groundwave.server.equipment.mapper.RelayInstallMapper;
import com.mindainfo.groundwave.server.equipment.mapper.RelayStateMapper;
import com.mindainfo.groundwave.server.interactive.redis.bean.ZJInfo;
import com.mindainfo.groundwave.server.member.bean.SysMember;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by MD-01 on 2017/7/5.
 */
@Service
@Transactional
public class RelayServiceImpl implements IRelayService {

    @Resource
    private RelayInfoMapper relayInfoMapper;
    @Resource
    private RelayInstallMapper relayInstallMapper;
    @Resource
    private IDeptService deptService;
    @Resource
    private RedissonClient redissonClient;

    /**
     * 页面查询列表
     */
    @NeedValidate
    public CommonResult<TableData<RelayVo.QueryListVo>> queryList(RelayParam.QueryListParam param){

        //获取当前用户的部门树
        CommonResult<List<Integer>> myDeptIdList = deptService.getMyDeptIdList();

        List<RelayInfo> relayInfos = relayInfoMapper.queryList(param.getStart(),
                param.getNum(), param.getKey(),myDeptIdList.getData());
        int count = relayInfoMapper.queryListCount(param.getKey(),myDeptIdList.getData());

        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        List<RelayVo.QueryListVo> vos = new ArrayList<>();
        RMap<String, ZJInfo> zjMap = redissonClient.getMap(RedisConstant.web_zj_hash);
        for(RelayInfo info : relayInfos){
            RelayVo.QueryListVo vo = new RelayVo.QueryListVo();
            vo.setRelayId(info.getId());
            vo.setSysNum(info.getSysNum());
            vo.setDeviceId(info.getDeviceId());
            vo.setVendor(info.getVendor());
            vo.setVersion(info.getVersion());
            vo.setMakerTime(format.format(info.getMakerTime()));
            vo.setUseState(info.getUseState());

            RelayInstall relayInstall = relayInstallMapper.queryBySysNum(info.getSysNum());
            if(relayInstall!=null){
                vo.setInstallPersion(relayInstall.getInstallPersion());
                vo.setInstallTime(format.format(relayInstall.getInstallTime()));
                vo.setLocation(relayInstall.getLocation());
                vo.setLng(relayInstall.getLng());
                vo.setLat(relayInstall.getLat());
            }
            ZJInfo zjInfo = zjMap.get(info.getDeviceId());
            if(zjInfo!=null){
                vo.setPower(zjInfo.getBatSt());
                vo.setAlarmState(zjInfo.getWarnSt());
            }
            vos.add(vo);
        }
        TableData<RelayVo.QueryListVo> data = new TableData<>();
        data.setRecordsFiltered(count);
        data.setRecordsTotal(count);
        data.setData(vos);
        return ResultFactory.ok(data);
    }


    /**
     * 添加设备
     */
    @NeedValidate
    public CommonResult<Void> add(RelayParam.AddParam param){

        RelayInfo info = relayInfoMapper.getBySysNum(param.getSysNum());
        if(info!=null){
            return ResultFactory.error("系统编号已经存在");
        }

        info = relayInfoMapper.queryByDeviceId(param.getDeviceId());
        if(info!=null){
            return ResultFactory.error("该设备编号已经存在");
        }

        info = new RelayInfo();
        info.setCreateTime(new Date());
        info.setDeviceId(param.getDeviceId());
        info.setMakerTime(param.getMakerTime());
        info.setUseState(0);
        info.setVendor(param.getVendor());
        info.setVersion(param.getVersion());
        SysMember member = SysUtil.currentMember();
        info.setDeptId(member.getDeptId());
        info.setSysNum(param.getSysNum());
        relayInfoMapper.insert(info);
        return ResultFactory.ok();
    }

    /**
     * 修改设备信息
     */
    @NeedValidate
    public CommonResult<Void> modify(RelayParam.ModifyParam param){

        RelayInfo info = relayInfoMapper.getBySysNum(param.getSysNum());
        if(info==null){
            return ResultFactory.error("系统编号错误");
        }
        info.setDeviceId(param.getDeviceId());
        info.setMakerTime(param.getMakerTime());
        info.setVendor(param.getVendor());
        info.setVersion(param.getVersion());
        relayInfoMapper.updateById(info);
        return ResultFactory.ok();
    }

    /**
     * 添加/修改 安装信息
     */
    @NeedValidate
    public CommonResult<Void> install(RelayParam.InstallParam param){

        RelayInfo info = relayInfoMapper.getBySysNum(param.getSysNum());
        if(info==null){
            return ResultFactory.error("错误的系统编号");
        }
        if(info.getUseState()==2){
            return ResultFactory.error("该设备已经停止使用");
        }

        RelayInstall install = relayInstallMapper.queryBySysNum(param.getSysNum());

        //没有安装信息
        if(install==null){
            install = new RelayInstall();
            install.setDeptId(info.getDeptId());
            install.setSysNum(param.getSysNum());
            install.setLocation(param.getLocation());
            install.setLng(param.getLng());
            install.setLat(param.getLat());
            install.setInstallTime(param.getInstallTime());
            install.setCreateTime(new Date());
            install.setInstallPersion(param.getPersionName());
            relayInstallMapper.insert(install);

            //添加安装信息之后,跟新设备的状态信息
            info.setUseState(1);

        }else{
            install.setLocation(param.getLocation());
            install.setLng(param.getLng());
            install.setLat(param.getLat());
            install.setInstallTime(param.getInstallTime());
            install.setInstallPersion(param.getPersionName());
            relayInstallMapper.updateById(install);
        }
        return ResultFactory.ok();
    }
}
